package com.zyf.service.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zyf.service.service.BusException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Data
@Component
public class StorageProperties {
    @Value("${videoPath}")
    public String location;

    public List videos() {
        return (loadAll());
    }

    /**
     * @Author: kech
     * @Description:遍历本地文件夹
     * @Date: 2021/12/31
     */
    public List<String> loadAll() {
        try {
            List<String> res = new ArrayList<>();
            File file = new File(location);
            if (null != file && file.isDirectory()) {
                File[] fs = file.listFiles();
                for (File f : fs) {
                    if (!f.isDirectory())
                        if (f.getName().contains(".m3u8") || f.getName().contains(".mp4")) {
                            if (f.length() < 1024 * 1024 * 50) {
                                res.add(f.getName());
                            }
                        }
                }
            }
            Collections.shuffle(res);
            return res;
        } catch (Exception e) {
            throw new BusException("Failed to read stored files");
        }
    }
}
